// 存储的分类数据
import { topCategory } from '@/api/constants'
import { findAllCategory } from '@/api/category'
// 分类模块
export default {
  namespaced: true,
  state () {
    return {
      // 分类信息集合
      list: topCategory.map((item) => ({ name: item }))
    }
  },
  mutations: {
    getAllCategory (state, result) {
      state.list = result
    },
    show (state, item) {
      // 使用find寻找和传入item匹配的元素，再把里面的状态值改成true
      const category = state.list.find(category => category.id === item.id)
      category.open = true
    },
    hide (state, item) {
      const category = state.list.find(category => category.id === item.id)
      category.open = false
    }
  },
  actions: {
    async getAllCategory (store) {
      const { result } = await findAllCategory()
      result.forEach((item) => {
        // 给里面添加一个属性，用作二级菜单展示和隐藏
        item.open = false
      })
      store.commit('getAllCategory', result)
    }
  }
}
